package com.tjec.project.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tjec.common.utils.DateUtils;
import com.tjec.common.utils.ServletUtils;
import com.tjec.framework.security.service.TokenService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tjec.project.system.mapper.BmsReportTypeMapper;
import com.tjec.project.system.domain.BmsReportType;
import com.tjec.project.system.service.IBmsReportTypeService;

/**
 * 报告类别Service业务层处理
 * 
 * @author tjec
 * @date 2020-07-22
 */
@Service
public class BmsReportTypeServiceImpl implements IBmsReportTypeService 
{
    @Autowired
    private BmsReportTypeMapper bmsReportTypeMapper;
    
    @Autowired
    private TokenService tokenService;
    
    /**
     * 查询报告类别
     * 
     * @param guid 报告类别ID
     * @return 报告类别
     */
    @Override
    public BmsReportType selectBmsReportTypeByGuid(String guid)
    {
        return bmsReportTypeMapper.selectBmsReportTypeByGuid(guid);
    }

    /**
     * 查询报告类别列表
     * 
     * @param bmsReportType 报告类别
     * @return 报告类别
     */
    @Override
    public List<BmsReportType> selectBmsReportTypeList(BmsReportType bmsReportType)
    {
        return bmsReportTypeMapper.selectBmsReportTypeList(bmsReportType);
    }

    /**
     * 新增报告类别
     * 
     * @param bmsReportType 报告类别
     * @return 结果
     */
    @Override
    public int insertBmsReportType(List<BmsReportType> bmsReportTypes)
    {
    	//1、先根据业务类别id查出已有报告类别集合
    	BmsReportType bmsReportType = new BmsReportType();
    	bmsReportType.setBusinessTypeId(bmsReportTypes.get(0).getBusinessTypeId());
    	List<BmsReportType> oldList = bmsReportTypeMapper.selectBmsReportTypeList(bmsReportType);
    	
    	if(oldList != null && oldList.size()>0) {//原报告类型不存在
    		
    		//不存在的报告类型
    		List<BmsReportType> insertList = new ArrayList<BmsReportType>();
    		//存在需要修改的报告类型
    		List<BmsReportType> updateList = new ArrayList<BmsReportType>();
    		//删除的报告类型
    		List<BmsReportType> deleteList = new ArrayList<BmsReportType>();
    		
    		
    		bmsReportTypes.forEach(item->{
    			if(item.getGuid()==null || "".equals(item.getGuid()) ) {
    				item.setCreateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
    				insertList.add(item);
    			}else {
    				oldList.forEach(it->{
    					if(item.getGuid().equals(it.getGuid()) && !item.getReportType().equals(it.getReportType())) {
    						item.setUpdateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
    						updateList.add(item);
    					}
    				});
    			}
    			
    		});
    		
    		oldList.forEach(item->{
    			List<String> strs = new ArrayList<String>();
    			bmsReportTypes.forEach(it->{
					if(item.getGuid().equals(it.getGuid())) {
						strs.add(it.getGuid());
					}
				});
    			
    			if(!strs.contains(item.getGuid())) {
    				deleteList.add(item);
    			}
    		});
    		
    		System.out.println("insertList="+insertList);
    		System.out.println("updateList="+updateList);
    		System.out.println("deleteList="+deleteList);
    		if(insertList.size()>0) {
    			insertList.forEach(item->{
    				bmsReportTypeMapper.insertBmsReportType(item);
    			});
    		}
    		
			if(updateList.size()>0) {
				updateList.forEach(item->{
					bmsReportTypeMapper.updateBmsReportType(item);
				});		
			}
			
			if(deleteList.size()>0) {
				deleteList.forEach(item->{
					bmsReportTypeMapper.deleteBmsReportTypeByGuid(item.getGuid());
				});	
			}
    		
    	}else {//直接添加
    		bmsReportTypes.forEach(item->{
    			item.setCreateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
    			bmsReportTypeMapper.insertBmsReportType(item);
    		});
    		
    	}
    	
    	//2、和现有报告类别集合对比
    	// 2.1 获取删除的报告类别
    	// 2.2 获取修改的报告类别
    	// 2.3 获取新增的报告类别
    	
    	
        //msReportType.setCreateTime(DateUtils.getNowDate());
    	//bmsReportTypeMapper.insertBmsReportType(bmsReportType)
        return 1;
    }

    /**
     * 修改报告类别
     * 
     * @param bmsReportType 报告类别
     * @return 结果
     */
    @Override
    public int updateBmsReportType(BmsReportType bmsReportType)
    {
        bmsReportType.setUpdateTime(DateUtils.getNowDate());
        return bmsReportTypeMapper.updateBmsReportType(bmsReportType);
    }

    /**
     * 批量删除报告类别
     * 
     * @param guids 需要删除的报告类别ID
     * @return 结果
     */
    @Override
    public int deleteBmsReportTypeByGuids(String[] guids)
    {
        return bmsReportTypeMapper.deleteBmsReportTypeByGuids(guids);
    }

    /**
     * 删除报告类别信息
     * 
     * @param guid 报告类别ID
     * @return 结果
     */
    @Override
    public int deleteBmsReportTypeByGuid(String guid)
    {
        return bmsReportTypeMapper.deleteBmsReportTypeByGuid(guid);
    }
    
    /**
     * 根据业务类别id删除报告类别信息
     * 
     * @param businessTypeId 业务类别ID
     * @return 结果
     */
    @Override
    public int deleteBmsReportTypeByBusinessTypeId(String businessTypeId)
    {
    	BmsReportType bmsReportType = new BmsReportType();
    	bmsReportType.setBusinessTypeId(businessTypeId);
    	List<BmsReportType> list = bmsReportTypeMapper.selectBmsReportTypeList(bmsReportType);
    	if(list != null && list.size()>0) {
    		return bmsReportTypeMapper.deleteBmsReportTypeByBusinessTypeId(businessTypeId);	
    	}else {
    		return 1;
    	}
    }
}
